import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'
const Home = () => import('@/views/home/index.vue')
const Article = () => import('@/views/article')
const Question = () => import('@/views/question')
const User = () => import('@/views/user')
const Chat = () => import('@/views/user/chat.vue')
const Profile = () => import('@/views/user/profile.vue')
const Video = () => import('@/views/video')
const Tabbar = () => import('@/components/app-tabbar.vue')
const Login = () => import('@/views/login/index.vue')

Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    alias: '/home',
    components: { default: Home, bottom: Tabbar }
  },
  {
    path: '/article',
    component: Article
  },
  {
    path: '/question',
    components: { default: Question, bottom: Tabbar }
  },
  {
    path: '/user/chat',
    component: Chat
  },
  {
    path: '/user/profile',
    component: Profile
  },
  {
    path: '/user',
    components: { default: User, bottom: Tabbar }
  },
  {
    path: '/video',
    components: { default: Video, bottom: Tabbar }
  },
  {
    path: '/login',
    component: Login
  }
]

const router = new VueRouter({
  routes
})
const whitePath = ['/user', '/user/chat', '/user/profile']
router.beforeEach((to, from, next) => {
  if (store.getters['user/getToken']) {
    if (to.path.toLowerCase() === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    if (whitePath.includes(to.path.toLowerCase())) {
      console.log(to.fullPath)
      next('/login?redirect=' + to.fullPath) // 路由传参
    } else {
      next()
    }
  }
})
export default router
